package thread.lock;
import thread.ThreadUtils;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
使用ReentrantLock改造原先的Ticket ReentrantLock类是一个可重入锁的类 实现了Lock接口
 */

public class Ticket2 implements Runnable{
    public  static Integer count = 100;
    public static Lock lock = new ReentrantLock();
    String name;

    public Ticket2(String name) {
        this.name = name;
    }



    @Override
    public  void run() {
        while (Ticket2.count>0){
            ThreadUtils.sleep(100);
            lock.lock();
            try {
                System.out.println(name + "出票一张还剩:" + count-- + "张");
            }finally {
                lock.unlock();
            }



        }
    }

    public  static void main(String[] args) {
        Thread t1 = new Thread(new Ticket2("1号窗口"));
        Thread t2 = new Thread(new Ticket2("2号窗口"));
        t1.start();
        t2.start();
    }

}
